package com.zhongan.gateway.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zhongan.gateway.bean.ApiPrivilege;
import com.zhongan.gateway.common.constant.GwDeveloperApiConstant;
import com.zhongan.gateway.mapper.ApiPrivilegeMapper;
import com.zhongan.gateway.service.ApiPrivilegeService;
import com.zhongan.gateway.service.RedisService;

@Component
public class ApiPrivilegeServiceImpl implements ApiPrivilegeService {

    private static final Logger logger = LoggerFactory.getLogger(ApiPrivilegeServiceImpl.class);
    @Resource
    private ApiPrivilegeMapper  apiPrivilegeMapper;

    @Resource
    private RedisService        redisService;

    @Override
    public int deletePrivilegeById(ApiPrivilege record) {
        String key = GwDeveloperApiConstant.GW_CACHE_PRIVILEDGE + record.getDeveloperId() + "_" + record.getApiId();
        redisService.clearCacheByKey(key);
        logger.info("start deletePrivilegeById,record={}", record.toString());
        int i = apiPrivilegeMapper.deleteByPrimaryKey(record.getId());
        logger.info("end deletePrivilegeById,i={}", i);
        return i;
    }

    @Override
    public int insertPrivilege(ApiPrivilege record) {
        String key = GwDeveloperApiConstant.GW_CACHE_PRIVILEDGE + record.getDeveloperId() + "_" + record.getApiId();
        redisService.clearCacheByKey(key);
        logger.info("start insertPrivilege, {}", record.toString());
        int i = apiPrivilegeMapper.insert(record);
        logger.info("end insertPrivilege,i={}", i);

        return i;
    }

    @Override
    public int updatePrivilege(ApiPrivilege record) {
        String key = GwDeveloperApiConstant.GW_CACHE_PRIVILEDGE + record.getDeveloperId() + "_" + record.getApiId();
        redisService.clearCacheByKey(key);
        logger.info("start updatePrivilege, {}", record.toString());
        int i = apiPrivilegeMapper.updateByPrimaryKey(record);
        logger.info("end updatePrivilege,i={}", i);

        return i;
    }

    @Override
    public ApiPrivilege selectPriById(Long id) {
        ApiPrivilege ap = apiPrivilegeMapper.selectByPrimaryKey(id);
        return ap;
    }

    @Override
    public List<ApiPrivilege> selectPrivileges(Long devId) {
        ApiPrivilege ap = new ApiPrivilege();
        ap.setDeveloperId(devId);
        List<ApiPrivilege> list = apiPrivilegeMapper.selectByParams(ap);
        return list;
    }

    @Override
    public List<ApiPrivilege> selectPrivileges(Long devId, Long apiId) {
        String key = GwDeveloperApiConstant.GW_CACHE_PRIVILEDGE + devId + "_" + apiId;
        Jedis jedis = redisService.getJedis();
        String result = redisService.get(jedis, key);
        List<ApiPrivilege> list = null;
        if (!StringUtils.isEmpty(result)) {
            list = (List<ApiPrivilege>) JSONObject.parseObject(result, new TypeReference<List<ApiPrivilege>>() {
            });
        } else {
            ApiPrivilege ap = new ApiPrivilege();
            ap.setDeveloperId(devId);
            ap.setApiId(apiId);
            list = apiPrivilegeMapper.selectByParams(ap);
            if (null != list && !list.isEmpty()) {
                redisService.set(jedis, key, JSONObject.toJSONString(list));
            }

        }
        redisService.closeJedis(jedis);
        return list;
    }

}
